/**
 * 
 */
package transactionExchange.pkg;
//Logging is required, make a class.
/**
 * @author Omar A. AlSadoon
 *
 */
public class Account {
	
	private final String accountKey;
	private final Customer accountHolder;
	private int errorCode;
	private float balance;
	private boolean transactionSuccess;


	public Account(String keyV,Customer customerV) {
		
		this.accountKey = keyV;
		this.accountHolder = customerV;
				
	}

	public int getErrorCode() {
		return this.errorCode;
	}

	private void setErrorCode(int errorCodeV){
		this.errorCode = errorCodeV;
	}
	
	protected String getAccountKey() {
		return accountKey;
	}
	
	protected Customer getHolder() {
		return accountHolder;
	}

	private synchronized boolean isBalanceCovered(float transactionAmountV) {
		
		if (this.balance - transactionAmountV > 0) {
			return true;
		}
		else {return false;}
	}
	
	private synchronized boolean withdrawFromBalance(float transactionAmountV) {
		
		this.balance =- transactionAmountV;
		return true; 
	}
	
	protected synchronized boolean depositToBalance(float transactionAmountV){
		
		this.balance =+ transactionAmountV;
		return true;
	}
	
	protected float getBalace(){
		return this.balance;
	}
	
	protected synchronized boolean transferTransaction(float transactionAmountV,Account beneficial){ //transferTo value is object of the other account to transfer to.
		
			//transactionAmountV = abs(transactionAmountV);//get absolute value;
			if (isBalanceCovered(transactionAmountV)) {
				if (withdrawFromBalance(transactionAmountV)) {
					if (beneficial.depositToBalance(transactionAmountV)) {
							 transactionSuccess = true;
							 setErrorCode(0); //success
					} else {this.depositToBalance(transactionAmountV); 
							transactionSuccess = false;
							setErrorCode(1001); //fault in depositing to beneficial account.
							}
				} else {transactionSuccess = false;
						setErrorCode(1002);//fault in withdrawing from account.
						}
			} else { transactionSuccess = false;
					 setErrorCode(1003);//not sufficient balance to make transaction.
					}
			
			return transactionSuccess;
		}

}
